/**
 * Collaborative Story Development Web Application (CSDApp) Copyright Anton
 * Strack 2014
 *
 * This file is part of Collaborative Story Development Application (CSDApp).
 *
 * CSDApp is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or any later version.
 *
 * CSDApp is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * CSDApp. If not, see <http://www.gnu.org/licenses/>.
 *
 */

/*
 * This file contains the event listeners and the respecive functions for csdapp operations
 */

/*
 * Setup the events to listen for and apply csdapp functions to.
 */
$(document).ready(function() {
    // delete sidebar displayed notification
    $('a.delete_notification').click(function() {
        return handleDeleteNotification($(this));
    });
});


/* -----------------------------------------------------------------------------------------------------
 * The remainder of this file contains javascript functions used by the csdapp for non-display purposes.
 * ----------------------------------------------------------------------------------------------------- */


/**
 * This function uses ajax to delete a notification from the sidebar without going to view the notification subject
 * @param {Object} element is the html element object that was clicked on.
 * @returns false to prevent event bubbling and default actions
 */
var handleDeleteNotification = function(element)
{
    // select the parent list element the delete link belongs to
    var li_element = element.closest('li.list-group-item');
    var element_id = li_element.attr("id");
    // extract just the id number from the li element id
    var id = element_id.substring(element_id.lastIndexOf("_") + 1);
    // post to server. no need to specifiy url
    $.post("",
            {
                action: "delete",
                resource: "notification",
                id: id,
                rtype: "async"
            },
    function(data) {
        console.log(data);
        if (data === "success")
        {
            li_element.fadeOut('fast');
        }
        else
        {
            alert("Could not delete notification.");
        }
    });
    return false;
};

/**
 * This function will check a post for having a list of elements. It is a simple check relying on the presense of li.posttype_relationshipId elements
 * @param {type} postType = the post type and relationship type that is acting as a class selector for the element.
 * @returns nothing
 */
var checkHasRelationship = function(postType)
{
    var count = $("li." + postType).length;
    if (count === 0)
    {
        var el = $("ul." + postType + "_relationship").closest("article");
        $(el).toggleClass("needRelationship");
        var aHref = $("ul." + postType + "_relationship").siblings("p").find("a").attr('href');
        $(el).prepend("<p class='needRelationship'>Please select the <a href='" + aHref + "'>Primary Speaker</a> for this conversation.</p>");
    }
};